home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 2 of 3.iso / chapter9 / progress.c < prev    next >
C/C++ Source or Header  |  1996-03-06  |  7KB  |  233 lines

  1.  
  2. #include <windows.h>  
  3. #include <commctrl.h>
  4. #include "progress.h"  
  5.  
  6.  
  7. #if defined (WIN32)
  8.     #define IS_WIN32 TRUE
  9. #else
  10.     #define IS_WIN32 FALSE
  11. #endif
  12.  
  13. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  14. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  15. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  16.  
  17. HINSTANCE hInst;   // current instance
  18.  
  19. LPCTSTR lpszAppName = "MyApp";
  20. LPCTSTR lpszTitle   = "Progress"; 
  21.  
  22.  
  23. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  24.  
  25.  
  26. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  27.                       LPTSTR lpCmdLine, int nCmdShow)
  28. {
  29.    MSG      msg;
  30.    HWND     hWnd; 
  31.    WNDCLASS wc;
  32.  
  33.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  34.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  35.    wc.cbClsExtra    = 0;                      
  36.    wc.cbWndExtra    = 0;                      
  37.    wc.hInstance     = hInstance;              
  38.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  39.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  40.    wc.hbrBackground = (HBRUSH)(COLOR_3DFACE+1);
  41.    wc.lpszMenuName  = lpszAppName;              
  42.    wc.lpszClassName = lpszAppName;              
  43.  
  44.    if ( IS_WIN95 )
  45.    {
  46.       if ( !RegisterWin95( &wc ) )
  47.          return( FALSE );
  48.    }
  49.    else if ( !RegisterClass( &wc ) )
  50.       return( FALSE );
  51.  
  52.    hInst = hInstance; 
  53.  
  54.    hWnd = CreateWindow( lpszAppName, 
  55.                         lpszTitle,    
  56.                         WS_OVERLAPPEDWINDOW, 
  57.                         CW_USEDEFAULT, 0, 
  58.                         CW_USEDEFAULT, 0,  
  59.                         NULL,              
  60.                         NULL,              
  61.                         hInstance,         
  62.                         NULL               
  63.                       );
  64.  
  65.    if ( !hWnd ) 
  66.       return( FALSE );
  67.  
  68.    ShowWindow( hWnd, nCmdShow ); 
  69.    UpdateWindow( hWnd );         
  70.  
  71.    while( GetMessage( &msg, NULL, 0, 0) )   
  72.    {
  73.       TranslateMessage( &msg ); 
  74.       DispatchMessage( &msg );  
  75.    }
  76.  
  77.    return( msg.wParam ); 
  78. }
  79.  
  80.  
  81. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  82. {
  83.    WNDCLASSEX wcex;
  84.  
  85.    wcex.style         = lpwc->style;
  86.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  87.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  88.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  89.    wcex.hInstance     = lpwc->hInstance;
  90.    wcex.hIcon         = lpwc->hIcon;
  91.    wcex.hCursor       = lpwc->hCursor;
  92.    wcex.hbrBackground = lpwc->hbrBackground;
  93.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  94.    wcex.lpszClassName = lpwc->lpszClassName;
  95.  
  96.    // Added elements for Windows 95.
  97.    //...............................
  98.    wcex.cbSize = sizeof(WNDCLASSEX);
  99.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  100.                             IMAGE_ICON, 16, 16,
  101.                             LR_DEFAULTCOLOR );
  102.             
  103.    return RegisterClassEx( &wcex );
  104. }
  105.  
  106.  
  107. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  108. {
  109. static HWND hProgress = NULL;
  110.  
  111.    switch( uMsg )
  112.    {
  113.       case WM_CREATE :
  114.               InitCommonControls();
  115.  
  116.               // Create the edit control.
  117.               //.........................
  118.               hProgress = CreateWindowEx( 0, PROGRESS_CLASS, "",
  119.                                           WS_CHILD | WS_VISIBLE,
  120.                                           40, 20, 200, 12, hWnd, 
  121.                                           (HMENU)1, hInst, NULL );
  122.  
  123.               break;
  124.  
  125.  
  126.       case WM_COMMAND :
  127.               switch( LOWORD( wParam ) )
  128.               {
  129.                  case IDM_TEST1 :
  130.                         {
  131.                            int i = 0;
  132.  
  133.                            // Reset the Progress bar.
  134.                            //........................
  135.                            SendMessage( hProgress, PBM_SETPOS, 0, 0 );
  136.  
  137.                            // Make sure the range is set.
  138.                            //............................
  139.                            SendMessage( hProgress, PBM_SETRANGE, 
  140.                                         0, MAKELPARAM(0,20) );
  141.  
  142.                            while( i<20 )
  143.                            {
  144.                               // Add delay to simulate a process.
  145.                               //.................................
  146.                               Sleep( 500 );
  147.  
  148.  
  149.                               // Increment the position.
  150.                               //........................
  151.                               i = SendMessage( hProgress, 
  152.                                                PBM_DELTAPOS, i+1, 0 );
  153.                            }
  154.                         }
  155.                         break;
  156.  
  157.                  case IDM_TEST2 :
  158.                         {
  159.                            int i = 0;
  160.  
  161.                            // Reset the Progress bar.
  162.                            //........................
  163.                            SendMessage( hProgress, PBM_SETPOS, 0, 0 );
  164.  
  165.                            // Make sure the range is set.
  166.                            //............................
  167.                            SendMessage( hProgress, PBM_SETRANGE, 
  168.                                         0, MAKELPARAM(0,20) );
  169.  
  170.                            // Set the step value to 2.
  171.                            //.........................
  172.                            SendMessage( hProgress, PBM_SETSTEP, 2, 0 );
  173.  
  174.                            while( i<18 )
  175.                            {
  176.                               // Add delay to simulate a process.
  177.                               //.................................
  178.                               Sleep( 500 );
  179.  
  180.  
  181.                               // Step the position.
  182.                               //...................
  183.                               i = SendMessage( hProgress, 
  184.                                                PBM_STEPIT, 0, 0 );
  185.                            }
  186.                         }
  187.                         break;
  188.  
  189.                  case IDM_ABOUT :
  190.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  191.                         break;
  192.  
  193.                  case IDM_EXIT :
  194.                         DestroyWindow( hWnd );
  195.                         break;
  196.               }
  197.               break;
  198.       
  199.       case WM_DESTROY :
  200.               PostQuitMessage(0);
  201.               break;
  202.  
  203.       default :
  204.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  205.    }
  206.  
  207.    return( 0L );
  208. }
  209.  
  210.  
  211. LRESULT CALLBACK About( HWND hDlg,           
  212.                         UINT message,        
  213.                         WPARAM wParam,       
  214.                         LPARAM lParam)
  215. {
  216.    switch (message) 
  217.    {
  218.        case WM_INITDIALOG: 
  219.                return (TRUE);
  220.  
  221.        case WM_COMMAND:                              
  222.                if (   LOWORD(wParam) == IDOK         
  223.                    || LOWORD(wParam) == IDCANCEL)    
  224.                {
  225.                        EndDialog(hDlg, TRUE);        
  226.                        return (TRUE);
  227.                }
  228.                break;
  229.    }
  230.  
  231.    return (FALSE); 
  232. }
  233.